package storage.v1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.units.Unit;
import com.onpositive.data.units.Units;
import com.onpositive.dataminer.core.DefaultUniverse;
import com.onpositive.mediawiki.propertyparser.PropertyCharacteristic;
import com.onpositive.semantic.model.api.labels.ILabelLookup;
import com.onpositive.semantic.model.api.labels.NotFoundException;
import com.onpositive.semantic.model.api.meta.DefaultMetaKeys;
import com.onpositive.semantic.model.api.meta.IHasMeta;
import com.onpositive.semantic.model.api.meta.IMeta;
import com.onpositive.semantic.model.api.meta.IServiceProvider;
import com.onpositive.semantic.model.api.meta.IWritableMeta;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.units.AbstractValueParser;
import com.onpositive.units.ParsedFileList;
import com.onpositive.units.ParsedLinkArray;

public class WikiStoragePropertyMeta implements IMeta {

	private PropertyCharacteristic pChar;

	public WikiStoragePropertyMeta(PropertyCharacteristic pChar) {

		this.pChar = pChar;
	}

	@Override
	public IMeta getMeta() {

		return this;
	}

	@Override
	public <T> T getSingleValue(String key, Class<T> requestedClass, Object ctx) {

		if (requestedClass.equals(String.class)) {
			
			if( key.equals("range") )
				return requestedClass.cast( pChar.getCharacteristic(PropertyCharacteristic.CHAR_PARSER_ID_LABEL) );
			
			return requestedClass.cast(pChar.getCharacteristic(key));
		}
		if( requestedClass.equals(boolean.class) || requestedClass.equals(Boolean.class) )
		{
			if( key.equals( PropertyCharacteristic.CHAR_IS_MULTIVALUE_LABEL ) )
				return requestedClass.cast( pChar.isMultivalue() );
			
			if( key.equals( PropertyCharacteristic.CHAR_DEPRICATED_LABEL ) ){
				boolean b = pChar.getDepricated();
				return requestedClass.cast( b );
			}
			if( key.equals( PropertyCharacteristic.CHAR_IS_IDENTIFICATOR ) ){
				boolean b = pChar.isIdent();
				return requestedClass.cast( b );
			}
			if( key.equals( IPropertyConstants.CHAR_IS_SERVICE_PROPERTY ) )
			{
				boolean b = pChar.isServiceProperty();
				return requestedClass.cast( b );
			}
		}
		String characteristic = pChar.getCharacteristic(key);
		return requestedClass.cast(characteristic);		
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T, A extends T> A getService(Class<T> requestedClass) {
		if (requestedClass==ILabelLookup.class){
			return (A) new ILabelLookup() {
				
				@Override
				public Object lookUpByLabel(IHasMeta model, Object parentObject, String label) throws NotFoundException
				{

					return ValuesExtractorV1.parseValue( Arrays.asList( new String[]{label} ) , WikiStoragePropertyMeta.this ,
						new IParserIdLookup() {
						
						@Override
						public String getParserId(String propertyName) {
							IProperty property = DefaultUniverse.getInstance().getProperty(null, pChar.getName());
							String stringValue = DefaultMetaKeys.getStringValue(property, PropertyCharacteristic.CHAR_PARSER_ID_LABEL);
							if (stringValue!=null){
								return stringValue;
							}
							return pChar.getCharacteristic( PropertyCharacteristic.CHAR_PARSER_ID_LABEL);
						}
					} ,
					DefaultParserProvider.getInstance(),
					"WikiStorageProperty");
				}
			};
		}
		return null;
	}

	@Override
	public Collection<Object> keys() {

		ArrayList<Object> result = new ArrayList<Object>( pChar.getCharacteristicsList() ) ;

		return result;
	}

	@Override
	public Collection<Class<?>> services() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IMeta getParentMeta() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IMeta getDefaultMeta() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getRevisionId() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public IWritableMeta getWritableCopy() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IServiceProvider<?> getDefaultServiceProvider() {
		// TODO Auto-generated method stub
		return null;
	}

}
